package action;

import java.util.ArrayList;
import java.util.List;
import model.Customer;
import persistency.CustomerRepository;
import persistency.PersistencyException;
import com.opensymphony.xwork2.ActionSupport;
import converter.MakePDF;

public class CustomerAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Customer> customers;
	private static CustomerRepository repository = new CustomerRepository();
	private List<String> typesCat;
	private List<String> typesCode;
	private List<String> states;
	private String editId;
	private String categoryCode;
	private int typeCode;
	private String name;
	private String street;
	private String number;
	private String mailbox;
	private String postalCode;
	private String city;
	private String telephone;
	private String email;
	private String iban;
	private String bic;
	private String active;
	private Customer customer;
	 
	public String doSave() {
		try {
			if (editId.equals("")) {
				Customer customer = new Customer();
				customer.setCategoryCode(categoryCode);
				customer.setTypeCode(typeCode);
				customer.setName(name);
				customer.setStreet(street);
				customer.setNumber(number);
				customer.setMailbox(mailbox);
				customer.setPostalCode(postalCode);
				customer.setCity(city);
				customer.setTelephone(telephone);
				customer.setEmail(email);
				customer.setIban(iban);
				customer.setBic(bic);	
				customer.setActive(Boolean.parseBoolean(active));
				repository.insert(customer); //inserting
			} else {
				Customer customer = repository.getById(editId);
				customer.setCategoryCode(categoryCode);
				customer.setTypeCode(typeCode);
				customer.setName(name);
				customer.setStreet(street);
				customer.setNumber(number);
				customer.setMailbox(mailbox);
				customer.setPostalCode(postalCode);
				customer.setCity(city);
				customer.setTelephone(telephone);
				customer.setEmail(email);
				customer.setIban(iban);
				customer.setBic(bic);	
				customer.setActive(Boolean.parseBoolean(active));				
				repository.update(customer); //updating
			} 
			try {
				customers = repository.getAll();
			} catch (PersistencyException e) {
				e.printStackTrace();
			}
			return "success";
			
		} catch (PersistencyException e) {
			addActionError("Customer save failed");
			return "input";
		}
		catch (Exception ex) {
			addActionError("Customer save failed");
			return "input";
		}
	}
	
	public String doList() {
		try {
			customers = repository.getAll();
		} catch (PersistencyException e) {
			
			e.printStackTrace();
		}
		return "SUCCESS";
	}
	
	public String input() {		
		typesCat = new ArrayList<String>();
		typesCat.add("Customer");
		typesCat.add("Company");
		
		typesCode = new ArrayList<String>();
		typesCode.add("Customer");
		typesCode.add("Supplier");
		typesCode.add("Headquarters");
		
		states = new ArrayList<String>();
		states.add("true");
		states.add("false");

		if (editId != "null" && !editId.equals("0")) {
			try {
				customer = repository.getById(editId);
			} catch (PersistencyException e) {
				e.printStackTrace();
			}
	}	

		return "input";
	}
	
	public String doMakepdf() {
		try {
			customers = repository.getAll();
		} catch (PersistencyException e) {
			e.printStackTrace();
		}
		return MakePDF.make();
	}
	
	
	public List<Customer> getCustomers() {
		return customers;
	}

	public Customer getCustomer() {
		return customer;
	}
	
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
	public List<String> getTypesCat() {
		return typesCat;
	}
	
	public List<String> getTypesCode() {
		return typesCode;
	}

	public List<String> getStates() {
		return states;
	}

	public String getCategoryCode() {
		return categoryCode;
	}

	public void setCategoryCode(String categoryCode) {
		this.categoryCode = categoryCode;
	}

	public int getTypeCode() {
		return typeCode;
	}

	public void setTypeCode(int typeCode) {
		this.typeCode = typeCode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getMailbox() {
		return mailbox;
	}

	public void setMailbox(String mailbox) {
		this.mailbox = mailbox;
	}

	public String getPostalCode() {
		return postalCode;
	}

	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getIban() {
		return iban;
	}

	public void setIban(String iban) {
		this.iban = iban;
	}

	public String getBic() {
		return bic;
	}

	public void setBic(String bic) {
		this.bic = bic;
	}
	
	public String getEditId() {
		return editId;
	}

	public void setEditId(String editId) {
		this.editId = editId;
	}

	public String getActive() {
		return active;
	}

	public void setActive(String active) {
		this.active = active;
	}



}
	
